﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Common;
using Domain;
using Domain.Officers;
using Service;

namespace UIComponentSprite.Building.Talents
{
    public partial class ApointOfficialPositionListForm : Form
    {
        public Domain.Building CurrentBuilding;
        public ApointOfficialPositionListForm(short buildId)
        {
            this.CurrentBuilding = Domain.Building.Buildings[buildId];
            InitializeComponent();
        }

        private void DispatchOfficerList_Load(object sender, EventArgs e)
        {
            CreateCustomColumns();
            DataBind();
            SetGridViewColumnStyle();

        }

        private void CreateCustomColumns()
        {
        }
        public void DataBind()
        {
            DataTable tb = CreateTable();
            foreach (KeyValuePair<short,OfficialPosition> item in OfficialPosition.OPositions)
            {
                List<Officer> list =
                    item.Value.CurrentOfficers.Where(x => x.BelongBuilding.ForceId == CurrentBuilding.ForceId).ToList();
                string name = "";
                foreach (var officer in list)
                {
                    name += officer.Name + ",";
                }
                DataRow dr = tb.NewRow();
                dr["ID"] = item.Key;
                dr["官位"] = item.Value.Name;
                dr["武将"] = name.TrimEnd(',');
                dr["武将功绩"] = list.Count==1?list.First().Performance.ToString():"";
                dr["参考功绩"] = item.Value.Performance==0?"":item.Value.Performance.ToString();
                dr["类型"] =item.Value.Type==0?"爵位":"官职";
                dr["分类"] = item.Value.Category==1?"文官":"武管";
                dr["多人"] =item.Value.IsMutilate?"是":"否";
                dr["政治"] = item.Value.PoliticsAdd == 0 ? "" : "+" + item.Value.PoliticsAdd.ToString();
                dr["统帅"] = item.Value.CommanderAdd == 0 ? "" : "+" + item.Value.CommanderAdd.ToString();
                dr["月俸"] = item.Value.Salary == 0 ? "" : item.Value.Salary.ToString();
                tb.Rows.Add(dr);
            }
            BindingSource bs = new BindingSource();
            bs.DataSource = tb;
            dataGridView1.DataSource = bs;

        }

        private void SetGridViewColumnStyle()
        {
            this.dataGridView1.Columns["ID"].Visible = false;
            this.dataGridView1.Columns["官位"].Width = 70;
            this.dataGridView1.Columns["武将"].Width = 60;
            this.dataGridView1.Columns["武将功绩"].Width = 70;
            this.dataGridView1.Columns["参考功绩"].Width = 70;
            this.dataGridView1.Columns["类型"].Width = 50;
            this.dataGridView1.Columns["分类"].Width = 50;
            this.dataGridView1.Columns["多人"].Width = 50;
            this.dataGridView1.Columns["政治"].Width = 50;
            this.dataGridView1.Columns["统帅"].Width = 50;
            this.dataGridView1.Columns["月俸"].Width = 50;
        }
        public DataTable CreateTable()
        {
            DataTable tb = new DataTable();
            tb.Columns.Add("ID", Type.GetType("System.Int16"));
            tb.Columns.Add("官位", Type.GetType("System.String"));
            tb.Columns.Add("武将", Type.GetType("System.String"));
            tb.Columns.Add("武将功绩", Type.GetType("System.String"));
            tb.Columns.Add("参考功绩", Type.GetType("System.String"));
            tb.Columns.Add("类型", Type.GetType("System.String"));
            tb.Columns.Add("分类", Type.GetType("System.String"));
            tb.Columns.Add("多人", Type.GetType("System.String"));
            tb.Columns.Add("政治", Type.GetType("System.String"));
            tb.Columns.Add("统帅", Type.GetType("System.String"));
            tb.Columns.Add("月俸", Type.GetType("System.String"));
            return tb;
        }
        /// <summary>
        /// 单元格点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e == null || e.Value == null || !(sender is DataGridView))
                return;
        }

        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
           
        }
        private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 2&& e.RowIndex != -1)
            {
                short positionId = (short)dataGridView1.Rows[e.RowIndex].Cells["ID"].Value;
                SelectOfficerToOfficialPositionListForm form = new SelectOfficerToOfficialPositionListForm(CurrentBuilding.Id, positionId);
                form.ShowDialog();
            }
        }

    }
}
